﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GolfLeague.ViewModels;

namespace GolfLeague.Models
{
    public class MatchInterface
    {
        GolfLeagueEntities golfDB = new GolfLeagueEntities();

        public IQueryable<Match> GetRoundMatches(int roundID)
        {
            return from matchEntity in golfDB.Matches
                   where matchEntity.RoundID.Equals(roundID)
                   orderby matchEntity.MatchTime
                   select matchEntity;
        }

        public IQueryable<TeamMatch> GetRoundTeamMatches(int roundID)
        {
            return from matchEntity in golfDB.TeamMatches
                   where matchEntity.Match.RoundID.Equals(roundID)
                   orderby matchEntity.Match.MatchTime
                   select matchEntity;
        }

        public IQueryable<PlayerMatch> GetRoundPlayerMatches(int roundID)
        {
            return from matchEntity in golfDB.PlayerMatches
                   where matchEntity.Match.RoundID.Equals(roundID)
                   orderby matchEntity.Match.MatchTime
                   select matchEntity;
        }

        public IQueryable<TeamMatch> GetMatchTeamMatches(int matchID)
        {
            return from matchEntity in golfDB.TeamMatches
                   where matchEntity.MatchID.Equals(matchID)
                   select matchEntity;
        }

        public Team GetPlayerMatchTeam(int matchID, int playerID)
        {
            return (from teamEntity in golfDB.TeamMatches
                    join playerEntity in golfDB.PlayerTeams
                    on teamEntity.TeamID equals playerEntity.TeamID
                    where teamEntity.MatchID.Equals(matchID) && playerEntity.PlayerID.Equals(playerID)
                    select teamEntity.Team).FirstOrDefault();
        }

        public IQueryable<Team> GetMatchTeams(int matchID)
        {
            return from teamEntity in golfDB.Teams
                   join matchEntity in golfDB.TeamMatches
                   on teamEntity.TeamID equals matchEntity.TeamID
                   where matchEntity.MatchID.Equals(matchID)
                   select teamEntity;
        }

        public IQueryable<PlayerMatch> GetMatchPlayerMatches(int matchID)
        {
            return from matchEntity in golfDB.PlayerMatches
                   where matchEntity.MatchID.Equals(matchID)
                   select matchEntity;
        }

        public Match GetMatch(int matchID)
        {
            return (from matchEntity in golfDB.Matches
                    where matchEntity.MatchID.Equals(matchID)
                    select matchEntity).FirstOrDefault();
        }

        public TeamMatch GetTeamMatch(int teamMatchID)
        {
            return (from matchEntity in golfDB.TeamMatches
                    where matchEntity.TeamMatchID.Equals(teamMatchID)
                    select matchEntity).FirstOrDefault();
        }

        public PlayerMatch GetPlayerMatch(int playerMatchID)
        {
            return (from matchEntity in golfDB.PlayerMatches
                    where matchEntity.PlayerMatchID.Equals(playerMatchID)
                    select matchEntity).FirstOrDefault();
        }

        public void AddMatch(Match match)
        {
            golfDB.Matches.AddObject(match);
        }

        public void AddPlayerMatch(PlayerMatch playerMatch)
        {
            golfDB.PlayerMatches.AddObject(playerMatch);
        }

        public void AddTeamMatch(TeamMatch teamMatch)
        {
            golfDB.TeamMatches.AddObject(teamMatch);
        }

        public void DeleteMatch(Match match)
        {
            golfDB.Matches.DeleteObject(match);
        }

        public void DeletePlayerMatch(PlayerMatch playerMatch)
        {
            golfDB.PlayerMatches.DeleteObject(playerMatch);
        }

        public void DeleteTeamMatch(TeamMatch teamMatch)
        {
            golfDB.TeamMatches.DeleteObject(teamMatch);
        }

        public void Save()
        {
            golfDB.SaveChanges();
        }

        public IEnumerable<MatchViewModel> GetMatchViewModels(int roundID)
        {
            IEnumerable<Match> matches = GetRoundMatches(roundID);
            
            List<MatchViewModel> matchViewModel = new List<MatchViewModel>();
            foreach (Match roundMatch in matches)
            {
                IEnumerable<Team> teams = GetMatchTeams(roundMatch.MatchID);
                IEnumerable<TeamMatch> teamPoints = GetMatchTeamMatches(roundMatch.MatchID);
                IEnumerable<PlayerMatch> playerMatches = GetMatchPlayerMatches(roundMatch.MatchID);
                decimal team1Points = teamPoints.First().Points;
                decimal team2Points = teamPoints.Last().Points;

                foreach (PlayerMatch playerMatch in playerMatches)
                    if (playerMatch.Player.PlayerTeams.Count(t => t.TeamID == teams.First().TeamID) > 0)
                        team1Points += (decimal)playerMatch.Points;
                    else
                        team2Points += (decimal)playerMatch.Points;

                matchViewModel.Add(new MatchViewModel
                {
                    MatchID = roundMatch.MatchID,
                    MatchTime = roundMatch.MatchTime,
                    Team1 = teams.First(),
                    Team1Points = team1Points,
                    Team2 = teams.Last(),
                    Team2Points = team2Points
                });
            }

            return matchViewModel;
        }
    }
}